package cn.agilean.test.service;

import cn.agilean.test.entity.Company;
import cn.agilean.test.entity.Product;
import cn.agilean.test.entity.Relation;
import cn.agilean.test.repo.BlackRepo;
import cn.agilean.test.repo.CompanyRepo;
import cn.agilean.test.repo.ProductRepo;
import cn.agilean.test.vo.CompanyVo;
import cn.agilean.test.vo.ProductVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class ProductManager {
    private ProductRepo productRepo;
    private CompanyRepo companyRepo;
    private BlackRepo blackRepo;

    public ProductManager(ProductRepo productRepo, CompanyRepo companyRepo, BlackRepo blackRepo) {
        this.productRepo = productRepo;
        this.companyRepo = companyRepo;
        this.blackRepo = blackRepo;
    }

    public ProductVo getByProductId(String id){
        ProductVo result = new ProductVo();
        CompanyVo companyVo = new CompanyVo();

        // 获取所有产品和公司
        List<Product> products = productRepo.getProducts();
        List<Company> companies = companyRepo.getCompanies();

        for(Product product:products){
            if(product.getId().equals(id)){
                result.setId(product.getId());
                result.setName(product.getName());
                result.setPrice(product.getPrice());
                companyVo.setId(product.getCompanyId());
            }
        }

        // 获取公司信息
        for(Company company:companies){
            if(company.getId().equals(companyVo.getId())){
                companyVo.setName(company.getName());
            }
        }
        result.setCompany(companyVo);
        return result;
    }

    public List<ProductVo> getAll(){
        List<ProductVo> result = new ArrayList<>();

        // 获取所有产品和公司
        List<Product> products = productRepo.getProducts();
        List<Company> companies = companyRepo.getCompanies();

        Map<String, Company> companyMap = companies.stream().collect(Collectors.toMap(Company::getId,
            t -> t));
        List<String> productBlackList = blackRepo.getProductBlackList();
        List<String> companyBlackList = blackRepo.getCompanyBlackList();

        for(Product product:products){
            if(productBlackList.contains(product.getId())){
                continue;
            }
            if(companyBlackList.contains(product.getCompanyId())){
                continue;
            }
            ProductVo productVo = new ProductVo();
            CompanyVo companyVo = new CompanyVo();

            productVo.setId(product.getId());
            productVo.setName(product.getName());
            productVo.setPrice(product.getPrice());
            companyVo.setId(product.getCompanyId());
            Company companyTemp = companyMap.get(product.getCompanyId());
            companyVo.setName(companyTemp.getName());
            productVo.setCompany(companyVo);
            result.add(productVo);
        }

        return result;
    }
}
